import httpFetch from 'share/httpFetch';
import config from 'config';
import constants from 'share/constants';
import errorMessage from "share/errorMessage";

export default {

  /**
   * @returns {*|PromiseLike<T | never>|Promise<T | never>}
   * @description 获取发票袋配置
   * @param params
   */
  getInvoiceBag(params) {
    return new Promise((resolve, reject) => {
      httpFetch.get(`${config.baseUrl}/api/expense/reports/bag/config/get`, params)
        .then((res) => {
          let statusObj = {};
          let total = Number(res.headers['x-total-count']) || 0;
          constants.expenseStatus.map((item) => {
            statusObj[item.value] = item.label;
          });
          res.data.map(bag => {
            bag.statueList = [];
            bag.labelsList = [];
            bag.statuses.map(item => {
              bag.statueList.push(statusObj[item]);
            });
            bag.labels.map(item => {
              bag.labelsList.push(item.name);
            });
          });
          resolve(res.data, total);
        })
        .catch(err => {
          errorMessage(err.response);
        });
    });
  },

  /**
   *
   * @param params
   * @returns {Promise<T | never>}
   * @description 保存发票袋的设置
   */
  saveInvoiceBagConfig(params) {
    return httpFetch.post(`${config.baseUrl}/api/expense/reports/bag/config`, params)
      .then((response) => {
        return response;
      });
  },
  /**
   *
   * @param userOID
   * @param params
   * @returns {Promise<T | never>}
   * @description 删除发票袋的设置
   */

  deleteInvoiceBagConfig(userOID) {
    return httpFetch.delete(`${config.baseUrl}/api/expense/reports/bag/config/delete/${userOID}`)
      .then((response) => {
        return response;
      });
  },
  /**
   *
   * @param params
   * @returns {*|PromiseLike<T | never>|Promise<T | never>}
   * @description  查看发票袋详情
   */
  getInvoiceBagDetail(params) {
    let url;
    params.id ? url = '/api/expense/reports/bag/config' : url = '/api/expense/reports/bag/config/get';
    return httpFetch.get(`${config.baseUrl + url}`, params)
      .then((response) => {
        return response;
      });
  },
  /**
   *
   * @param params
   * @returns {*|PromiseLike<T | never>|Promise<T | never>}
   * @description  校验首位是否被占用
   */
  checkPrefix(params) {
    return httpFetch.get(`${config.baseUrl}/api/expense/reports/bag/prefix/check`, params)
      .then((response) => {
        return response;
      });
  },

  //取消拆单配置
  cancelSplitProfile() {
    return httpFetch.put(`${config.baseUrl}/api/function/profiles/remove/splitProfile`)
  },

};
